using System;
using System.Web;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Common;
using Sedna.Core.Persistence;

namespace Sedna.Core.Controls.Actions.Support
{

	class BaseHbmFilterAction : AbstractAction
	{

		private IHqlExecutor hqlExecutor;
		private string filterStatePath = "filterState";


		public IHqlExecutor HqlExecutor
		{
			set { hqlExecutor = value; }
		}


		public string FilterStatePath
		{
			set { filterStatePath = value; }
		}


		public override IActionResult Execute(IExecutionContext executionContext)
		{
			IFilterState filter = GetFilter(executionContext);
			SetupSorting(filter, executionContext);
			SetupPaging(filter, executionContext);
			IQueryResult result = GetResultSet(filter);

			//object virtualItemCount = executionContext[executionContext.CurrentExecutionContextPrefix + ".virtual-item-count"];
//			executionContext[executionContext.CurrentExecutionContextPrefix + ".virtual-item-count"] = 
//				result.IsTotalCountDefined ? result.TotalCount : result.Data.Count;

			executionContext.SetToCurrentScope("data", result.Data);
			return CreateResult(successStatus);
		}


		protected virtual IQueryResult GetResultSet(IFilterState filter)
		{
			IQueryResult result = hqlExecutor.Execute(filter);
			return result;
		}


		protected virtual IFilterState GetFilter(IExecutionContext executionContext)
		{
			IFilterState filter = (IFilterState)executionContext[filterStatePath];
			return filter;
		}


		protected virtual void SetupSorting(IFilterState filter, IExecutionContext executionContext)
		{
			if(filter == null || executionContext == null)
			{
				return;
			}

//			string executionContextPrefix = executionContext.CurrentExecutionContextPrefix;
//			if(executionContextPrefix == null || String.Empty.Equals(executionContextPrefix))
//			{
//				return;
//			}
//
//			string expression = (string)executionContext[executionContextPrefix + ".sort-expression"];
//			if(expression == null || String.Empty.Equals(expression))
//			{
//				return;
//			}
//
//			filter.SortExpression = expression;
//			filter.IsAsc = GetSortAsc(executionContext, executionContextPrefix);
		}


		protected virtual void SetupPaging(IFilterState filter, IExecutionContext executionContext)
		{
			if(filter == null || executionContext == null)
			{
				return;
			}

//			string executionContextPrefix = executionContext.CurrentExecutionContextPrefix;
//			if(executionContextPrefix == null || String.Empty.Equals(executionContextPrefix))
//			{
//				return;
//			}
//
//			object pageSizeObj = executionContext[executionContextPrefix + ".page-size"];
//			if(pageSizeObj == null)
//			{
//				return;
//			}
//
//			object currentPageIndexObj = executionContext[executionContextPrefix + ".current-page-index"];
//			if(currentPageIndexObj == null)
//			{
//				return;
//			}
//
//			filter.Amount = (int)pageSizeObj;
//			filter.StartIndex = (int)currentPageIndexObj;
		}


		private static bool GetSortAsc(IExecutionContext executionContext, string executionContextPrefix)
		{
			bool sortAsc = true;
			object sortAscObj = executionContext[executionContextPrefix + ".sort-asc"];
			if(sortAscObj != null)
			{
				sortAsc = (bool)sortAscObj;
			}
			return sortAsc;
		}



	}

}